#include "Metadata.h"

#ifndef MENOS_UNO
	#define MENOS_UNO -1
#endif

#ifndef ZERO
	#define ZERO 0
#endif

#ifndef UNO
	#define UNO 1
#endif

Metadata* Metadata::GetInstance()
{
	static Metadata instance;
	return &instance;
}

Metadata::Metadata()
{
}

Metadata::~Metadata()
{
	for(std::vector<MetadataOrganization*>::iterator it = organizations.begin(); it != organizations.end(); it++)
	{
		if((*it) != NULL)
			delete (*it);
		(*it)=NULL;
	}
	organizations.clear();
}

bool Metadata::is(int handler)
{
	std::vector<MetadataOrganization*>::iterator it = organizations.begin();
	bool find = false;

	while(it != organizations.end() && !find)
	{
		MetadataOrganization* temp = (*it); 
		if(temp->getHandler() == handler)
		{
			find = true;
		}
		else
			it++;
	}
	return find;
}

MetadataOrganization * Metadata::get(int handler)
{
	std::vector<MetadataOrganization*>::iterator it = organizations.begin();
	bool find = false;
	MetadataOrganization * organization = NULL;

	while(it != organizations.end() && !find)
	{
		MetadataOrganization* temp = (*it); 
		if(temp->getHandler() == handler)
		{
			organization = temp;
			find = true;
		}
		else
			it++;
	}
	return organization;
}

void Metadata::add(MetadataOrganization *organization)
{
	organizations.push_back(organization);		
}

void Metadata::update(MetadataOrganization *organization)
{
	std::vector<MetadataOrganization*>::iterator it = organizations.begin();
	bool find = false;
	unsigned int pos = ZERO, i = ZERO;
	MetadataOrganization* temp = NULL;
	int handler = organization->getHandler();
	
	while(it != organizations.end() && !find)
	{
		temp = (*it);
		if(temp->getHandler() == handler)
		{
			pos = i;
			find = true;
		}
		else
		{
			it++;
			i++;
		}
	}
	if(find)
	{
		it = organizations.begin() + pos;
		delete (*it);
		(*it) = organization;
	}
}



void Metadata::remove(int handler)
{
	std::vector<MetadataOrganization*>::iterator it = organizations.begin();
	unsigned int pos = ZERO;
	bool find = false;
	MetadataOrganization* temp = NULL;

	if(handler > MENOS_UNO)
	{
		while(it != organizations.end() && !find)
		{
			temp = (*it); 
			if(temp->getHandler() == handler)
				find = true;
			else
			{
				it++;
				pos++;
			}
		}
		if(find)
		{
			it = organizations.begin() + pos;
			delete (*it);
			organizations.erase(it);
		}
	}
}

void Metadata::load(MetadataOrganization *organization)
{	
	if(organization != NULL)
	{
		organization->load();
		if(this->is(organization->getHandler()))
			this->update(organization);
		else
			this->add(organization);
		
	}
	

}

void Metadata::save(int handler)
{
	std::vector<MetadataOrganization*>::iterator it = organizations.begin();
	bool find = false;
	
	if(handler > ZERO)
	{
		while(it != organizations.end() && !find)
		{
			MetadataOrganization* temp = (*it); 
			if(temp->getHandler() == handler)
			{
				temp->save();
				find = true;
			}
			else
				it++;
		}
	}
}
	
void Metadata::save(MetadataOrganization *organization)
{
	if(organization != NULL)
	{
		organization->save();
	}
}

void Metadata::saveAll()
{
	std::vector<MetadataOrganization*>::iterator it = organizations.begin();
	bool find = false;

	while(it != organizations.end() && !find)
	{
		MetadataOrganization* temp = (*it); 
		temp->save();
		it++;
	}
	
}

